<template>
  <el-dialog
    v-model="dialogVisable"
    title="新建组织架构"
    :append-to-body="true"
    @close="closeDialog"
    :destroy-on-close="true"
    width="600"
  >
    <el-form
      :model="addOrgForm"
      :rules="addOrgFormrules"
      ref="addOrgFormRef"
      label-width="100px"
    >
      <el-form-item
        v-if="addOrgForm.parentId?.length === 0 ? false : true"
        label="所属父组织"
        label-width="100px"
      >
        <el-input disabled v-model="addOrgForm.parentName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="组织名称" prop="displayName">
        <el-input v-model="addOrgForm.displayName" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div class="modal-footer">
      <el-button type="primary" @click="saveOrg">保存</el-button>
    </div>
  </el-dialog>
</template>
<script setup lang="ts">
import { addOrganization } from "@/api/identity/organizationUnits/index";
const addOrgForm = ref<any>({});
const addOrgFormrules = ref<FormRules>();
addOrgFormrules.value = {
  displayName: [{ required: true, message: "请输入组织架构名称", trigger: "blur" }],
};
const addOrgFormRef = ref<FormInstance>();
const dialogVisable = ref<boolean>(true);
const props = defineProps({

  currentNode:{type:Object,default:null}
});

const emit = defineEmits(["closedialogevent", "refresh"]);

const closeDialog = () => {
  dialogVisable.value = false;
  emit("closedialogevent", dialogVisable.value);
};

const saveOrg = () => {
  let currentNode=props.currentNode;
  addOrgFormRef.value?.validate((valid, fields) => {
    if (valid) {
      let param = {
        parentId: addOrgForm.value.parentId!=null ? addOrgForm.value.parentId:null,
        displayName: addOrgForm.value.displayName,
      };

      addOrganization(param).then((res) => {
        if (res) {
          ElNotification({
            title: "提示",
            message: "添加组织架构成功",
            type: "success",
            duration: 2000,
          });
          closeDialog();
          emit("refresh");
        }
      });
    }
  });
};
onMounted(() => {
  dialogVisable.value = true;
  addOrgForm.value.parentId =props.currentNode.data!=null? props.currentNode.data.id:null;
  addOrgForm.value.parentName = props.currentNode.data!=null? props.currentNode.data.displayName:null;
  console.log(props);
});
</script>
